﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.commands;
using Microsoft.Win32;
using InHD.utilities;
using InHD.utilities.enums;

namespace InHD.viewmodels
{
    class StaffFormViewModel : WorkspaceViewModel, IDataErrorInfo
    {
        #region Constructor
        public StaffFormViewModel(ActionModes mode, Staff staff)
        {
            FormMode = mode;
            _staff = staff;
        }
        #endregion

        #region Value Property
        private Staff _staff;
        public string commandExcute = string.Empty;
        bool CanDelete
        {
            get { return !string.IsNullOrEmpty(this._staff.StaffUNK)? true : false; }
        }

        public ActionModes FormMode
        {
            get;
            set;
        }

        #endregion

        #region Presentation Property

        public Staff Staff
        {
            get
            {
                return _staff;
            }
        }

        public string StaffCode
        {
            get { return _staff.StaffCode; }
            set
            {
                if (value == _staff.StaffCode)
                    return;

                _staff.StaffCode = value;

                base.OnPropertyChanged("StaffCode");
            }
        }

        public string Username
        {
            get { return _staff.Username; }
            set
            {
                if (value == _staff.Username)
                    return;

                _staff.Username = value;

                base.OnPropertyChanged("Username");
            }
        }


        public string Fullname
        {
            get { return _staff.Fullname; }
            set
            {
                if (value == _staff.Fullname)
                    return;

                _staff.Fullname = value;

                base.OnPropertyChanged("Fullname");
            }
        }

        public string Phone
        {
            get { return _staff.Phone; }
            set
            {
                if (value == _staff.Phone)
                    return;

                _staff.Phone = value;

                base.OnPropertyChanged("Phone");
            }
        }

        public string Email
        {
            get { return _staff.Email; }
            set
            {
                if (value == _staff.Email)
                    return;

                _staff.Email = value;

                base.OnPropertyChanged("Email");
            }
        }

        public string Address
        {
            get { return _staff.Address; }
            set
            {
                if (value == _staff.Address)
                    return;

                _staff.Address = value;

                base.OnPropertyChanged("Address");
            }
        }

        public StaffTypes SType
        {
            get { return _staff.SType; }
            set
            {
                if (value == _staff.SType)
                    return;

                _staff.SType = value;

                base.OnPropertyChanged("SType");
            }
        }

        public DateTime LastAccess
        {
            get { return _staff.LastAccess; }
        }

        #endregion

        #region IDataErrorInfo Interface
        string IDataErrorInfo.Error
        {
            get { return (_staff as IDataErrorInfo).Error; }
        }

        string IDataErrorInfo.this[string propertyName]
        {
            get
            {
                string error = null;

                error = (_staff as IDataErrorInfo)[propertyName];


                // Dirty the commands registered with CommandManager,
                // such as our Save command, so that they are queried
                // to see if they can execute now.
                CommandManager.InvalidateRequerySuggested();

                return error;
            }
        }
        #endregion

        #region Command
        private RelayCommand _closeCommand;
        public ICommand CancelCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new RelayCommand(
                        (param) => this.Close());
                }
                return _closeCommand;
            }
        }

        private RelayCommand _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(
                        (param) => this.Save(), (param) => this._staff.IsValid);
                }
                return _saveCommand;
            }
        }

        private RelayCommand _deleteCommand;
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    _deleteCommand = new RelayCommand(
                        (param) => this.Delete(), (param)=>this.CanDelete);
                }
                return _deleteCommand;
            }
        }

        #endregion

        #region Command Method
        void Close()
        {
            commandExcute = "Close";
            OnRequestClose();
        }

        void Save()
        {
            commandExcute = "Save";
            OnRequestClose();
        }

        void Delete()
        {
            commandExcute = "Delete";
            OnRequestClose();
        }
        #endregion
    }
}
